package com.bak;

import java.awt.*;
import java.util.Random;

public class Tank {
    public static final int WIDTH = ResourceManager.tankD.getWidth();
    public static final int HEIGHT = ResourceManager.tankD.getHeight();
    public static int SPEED = Integer.parseInt(PropertiesManager.getValue("TANK_SPEED"));
    private int x;
    private int y;
    private Dir dir = Dir.D;
    private boolean moving = false;
    private boolean live = true;
    private Group group = Group.BAD;
    private TankFrame tankFrame;
    private Random random = new Random();
    private Rectangle rectangle;

    public Tank() {

    }

    public Tank(int x, int y, Group group, boolean isMoving, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.group = group;
        this.moving = isMoving;
        this.tankFrame = tankFrame;
        this.rectangle = new Rectangle(x, y, WIDTH, HEIGHT);
    }

    public Rectangle getRectangle() {
        return rectangle;
    }

    public void setRectangle(Rectangle rectangle) {
        this.rectangle = rectangle;
    }

    public void paint(Graphics g) {
        if (!isLive()) {
            tankFrame.getTankList().remove(this);
            return;
        }
        move();
        switch (dir) {
            case U:
                g.drawImage(ResourceManager.tankU, x, y, null);
                break;
            case D:
                g.drawImage(ResourceManager.tankD, x, y, null);
                break;
            case L:
                g.drawImage(ResourceManager.tankL, x, y, null);
                break;
            case R:
                g.drawImage(ResourceManager.tankR, x, y, null);
                break;
            case LU:
                g.drawImage(ResourceManager.tankLU, x, y, null);
                break;
            case RU:
                g.drawImage(ResourceManager.tankRU, x, y, null);
                break;
            case RD:
                g.drawImage(ResourceManager.tankRD, x, y, null);
                break;
            case LD:
                g.drawImage(ResourceManager.tankLD, x, y, null);
                break;
        }

        this.rectangle.x = x;
        this.rectangle.y = y;

    }


    public void move() {
        if (moving) {
            switch (dir) {
                case U:
                    y -= SPEED;
                    break;
                case D:
                    y += SPEED;
                    break;
                case L:
                    x -= SPEED;
                    break;
                case R:
                    x += SPEED;
                    break;
                case LU:
                    x -= SPEED;
                    y -= SPEED;
                    break;
                case RU:
                    x += SPEED;
                    y -= SPEED;
                    break;
                case RD:
                    x += SPEED;
                    y += SPEED;
                    break;
                case LD:
                    x -= SPEED;
                    y += SPEED;
                    break;
            }

            if (random.nextInt(100) > 95 && group == Group.BAD) randomDir();

            checkBound();

        }
        //随机发射，只让敌方阵容自动攻击
        if (random.nextInt(10) > 5 && group == Group.BAD) {
            fire();
        }


    }

    private void randomDir() {
        int randDirNum = random.nextInt(Dir.values().length);
        for (Dir value : Dir.values()) {
            if (value.ordinal() == randDirNum) {
                dir = value;
                return;
            }
        }

    }

    private void checkBound() {
        if (x > TankFrame.GAME_WIDTH - WIDTH) {
            x = TankFrame.GAME_WIDTH - WIDTH;
        }
        if (x < 0) {
            x = 0;
        }
        if (y > TankFrame.GAME_HEIGHT - HEIGHT) {
            y = TankFrame.GAME_HEIGHT - HEIGHT;
        }
        if (y < 28) {
            y = 28;
        }
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public void fire() {
        //子弹出现于坦克的正中心
        //就写了一个斜方向的 测试一下就完事了
        if (dir == Dir.RU) {
            tankFrame.getBulletList().add(new Bullet(x + Tank.WIDTH + 10, y + Tank.HEIGHT + Bullet.HEIGHT - Bullet.HEIGHT - 60, dir, group, tankFrame));
            return;

        }
        int bX = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
        int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
        tankFrame.getBulletList().add(new Bullet(bX, bY, dir, group, tankFrame));

    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }

    public void die() {
        this.live = false;
    }


}
